package com.sec.android.app.myfiles.feature;

import android.content.Context;
import android.database.Cursor;

import com.sec.android.app.myfiles.feature.optimizestorage.DuplicatedFileFinder;
import com.sec.android.app.myfiles.feature.optimizestorage.FileFinder;
import com.sec.android.app.myfiles.feature.optimizestorage.FileSystemFinder;
import com.sec.android.app.myfiles.feature.optimizestorage.MediaProviderFinder;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.OptimizeStorageType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by daesu83.kim on 2016-10-19.
 * Original code is made by jw7.jeong.
 */
public class OptimizeStorageMgr {

    public enum StorageDbType {
        OPTIMIZE_STORAGE,
        MEDIA_PROVIDER,
        CLOUD_GOOGLE_DRIVE,
        CLOUD_SAMSUNG_DRIVE,
        WRAP_MEDIA_PROVIDER;

        public boolean isCloud() {
            return this == CLOUD_GOOGLE_DRIVE || this == CLOUD_SAMSUNG_DRIVE;
        }
    }

    private static OptimizeStorageMgr sInstance;
    private Context mContext;

    public static OptimizeStorageMgr getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new OptimizeStorageMgr();
        }
        sInstance.mContext = context;
        return sInstance;
    }

    private OptimizeStorageMgr() {
    }

    /**
     * Synchronous method.
     * @param type
     * @return
     */
    public List<AnalysisResult> getAnalysisResult(final OptimizeStorageType type) {
        List<AnalysisResult> ret = new ArrayList<>();
        List<FileFinder> finders = FinderFactory.getFinder(mContext, type);
        if (finders != null) {
            for (FileFinder finder : finders) {
                finder.find(ret);
            }
        }
        return ret;
    }

    public static class AnalysisResult {
        public OptimizeStorageType mType;
        public StorageDbType mStorageDbType;
        public Cursor mCursor;
    }

    private static class FinderFactory {
        public static List<FileFinder> getFinder(Context context, OptimizeStorageType type) {
            List<FileFinder> ret = new ArrayList<>();
            switch (type) {
                case LargeFiles:
                /*FALL THROUGH */
                case UnusedFiles:
                    ret.add(new MediaProviderFinder(context, type));
                    break;

                case UnnecessaryData:
                    ret.add(new MediaProviderFinder(context, type));
                    ret.add(new FileSystemFinder(type));
                    break;

                case DuplicateFiles:
                    ret.add(new DuplicatedFileFinder(context));
                    break;
            }
            return ret;
        }
    }
}
